import {HttpClient} from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import {ClientCompensationProvider} from "../client-compensation/client-compensation.provider";

@Injectable()
export class ClientRenewalProvider {
    prePath: string;

    constructor(public http: HttpClient) {
        this.prePath = 'party/v1';
    }

    /**
     * 获取续期客户
     * @param {number} type 还有多少天续期，默认90天
     * @param {number} notLoading 不需要加载动画取值为 0表示需要加载动画，1表示不需要加载动画
     * @param {number} pageParam 分页参数
     * @returns {Observable<any>}
     */
    getRenewalList(type: number = 60, notLoading = 0, pageParam, notLoadingNow = 0): Observable<any> {
        //pageNum:number=1,pageSize:number=20
        const p = `renewalClient`;
        const url = `/${this.prePath}/${p}`;
        return this.http.get(url, {
            params: {
                'not-loading': '' + notLoading,
                'not-loading-now': '' + notLoadingNow,
                type: '' + type,
                pageNum: '' + pageParam.pageNum,
                pageSize: '' + pageParam.pageSize
            }
        });
    }

    momentOperate(moment) {
        const month = moment.month();
        const date = moment.date();
        return {
            moment,
            monthDescribe: month ? `${month}月` : '',
            dateDescribe: date ? `${date}天` : '',
            toString: function () {
                return `${this.monthDescribe}${this.dateDescribe}`;
            }
        }
    }

    /**
     * 过滤为空的电话号码
     * @param clientList
     */
    checkMobileNull(clientList) {
        clientList.forEach(client => {
            const mobileList = [];
            if (client.mobilePhone && client.mobilePhone.length) {
                client.mobilePhone.forEach(mobile => {
                    if (mobile && mobile.mobile) {
                        mobileList.push({
                            id: mobile.id,
                            mobile: mobile.mobile
                        });
                    }
                });
            }
            client.mobilePhone = mobileList;
        });
        return clientList;
    }

}
